<template>
  <div class="container">
    <Breadcrumb
      :items="[
        'menu.pay.internetofthings',
        'menu.pay.internetofthings.carpark',
        'menu.pay.internetofthings.carparkuselog',
      ]"
    />
    <a-card
      class="general-card"
      :title="$t('menu.pay.internetofthings.carparkuselog')"
    >
      <a-row>
        <a-col :flex="1">
          <a-form
            :model="formModel"
            :label-col-props="{ span: 6 }"
            :wrapper-col-props="{ span: 18 }"
            label-align="left"
          >
            <a-row :gutter="16">
              <a-col :span="8">
                <a-form-item field="numberPlate" label="车牌号">
                  <a-input
                    v-model="formModel.numberPlate"
                    placeholder="请输入"
                    allow-clear
                  />
                </a-form-item>
              </a-col>
              <a-col :span="8">
                <a-form-item field="carParkId" label="停车场">
                  <!-- <a-input
                    v-model="formModel.carParkId"
                    placeholder="请输入"
                    allow-clear
                  /> -->
                  <a-select
                    :loading="loading"
                    placeholder="请输入"
                    allow-search
                    allow-clear
                    @search="handleSelectSearch"
                    :filter-option="false"
                    v-model="formModel.carParkId"
                  >
                    <a-option v-for="item of parkList" :value="item.id">{{
                      item.name
                    }}</a-option>
                  </a-select>
                </a-form-item>
              </a-col>
              <a-col :span="8">
                <a-form-item label="车辆类型" field="carType">
                  <a-select
                    v-model="formModel.carType"
                    placeholder="请选择"
                    allow-clear
                  >
                    <a-option
                      :value="item.flagKey"
                      v-for="item in carTypeList"
                      :key="item.id"
                      >{{ item.name }}</a-option
                    >
                  </a-select>
                </a-form-item>
              </a-col>
              <a-col :span="8">
                <a-form-item label="出场时间大于" field="minOutTime">
                  <a-date-picker
                    show-time
                    format="YYYY-MM-DD HH:mm:ss"
                    placeholder="请选择"
                    v-model="formModel.minOutTime"
                  />
                </a-form-item>
              </a-col>
              <a-col :span="8">
                <a-form-item label="出场时间小于" field="maxOutTime">
                  <a-date-picker
                    show-time
                    format="YYYY-MM-DD HH:mm:ss"
                    placeholder="请选择"
                    v-model="formModel.maxOutTime"
                  />
                </a-form-item>
              </a-col>
            </a-row>
          </a-form>
        </a-col>
        <a-divider style="height: 84px" direction="vertical" />
        <a-col :flex="'86px'" style="text-align: right">
          <a-space direction="vertical" :size="18">
            <a-button type="primary" @click="search">
              <template #icon>
                <icon-search />
              </template>
              {{ $t('searchTable.form.search') }}
            </a-button>
            <a-button @click="reset">
              <template #icon>
                <icon-refresh />
              </template>
              {{ $t('searchTable.form.reset') }}
            </a-button>
          </a-space>
        </a-col>
      </a-row>
      <a-divider style="margin-top: 0" />
      <a-row style="margin-bottom: 16px">
        <a-col :span="12">
          <a-space>
            <!-- <a-button type="primary" @click="handleAdd">
              <template #icon>
                <icon-plus />
              </template>
              {{ $t('searchTable.operation.create') }}
            </a-button> -->
            <a-upload action="/">
              <template #upload-button>
                <a-button>
                  {{ $t('searchTable.operation.import') }}
                </a-button>
              </template>
            </a-upload>
            <a-button type="primary" status="danger" @click="onBatchDelete"
              >删除</a-button
            >
          </a-space>
        </a-col>
        <a-col
          :span="12"
          style="display: flex; align-items: center; justify-content: end"
        >
          <a-button>
            <template #icon>
              <icon-download />
            </template>
            {{ $t('searchTable.operation.download') }}
          </a-button>
        </a-col>
      </a-row>
      <a-table
        row-key="id"
        :columns="columns"
        :data="state.list"
        :style="{ height: '700px' }"
        :pagination="pagination"
        :loading="loading"
        @page-change="pageChange"
        :row-selection="rowSelection"
        v-model:selectedKeys="selectedKeys"
      >
        <template #index="{ rowIndex }">
          {{ rowIndex + 1 + (pagination.current - 1) * pagination.pageSize }}
        </template>
        <!-- <template #operations="{ record }">
          <a-space>
            <a-popconfirm content="您确定删除吗？" @ok="onDelete(record.id)">
              <a-button type="primary" status="danger" size="mini"
                >删除</a-button
              >
            </a-popconfirm>
          </a-space>
        </template> -->
        <template #inSmallImageUrl="{ record }">
          <a-image
            :src="record.inSmallImageUrl"
            :width="80"
            :height="30"
          ></a-image>
        </template>
        <template #inBigImageUrl="{ record }">
          <a-image
            :src="record.inBigImageUrl"
            :width="80"
            :height="30"
          ></a-image>
        </template>
        <template #outSmallImageUrl="{ record }">
          <a-image
            :src="record.outSmallImageUrl"
            :width="80"
            :height="30"
          ></a-image>
        </template>
        <template #outBigImageUrl="{ record }">
          <a-image
            :src="record.outBigImageUrl"
            :width="80"
            :height="30"
          ></a-image>
        </template>
      </a-table>
    </a-card>
    <editAdd ref="editAddRef" @fetch-data="fetchData" />
  </div>
</template>

<script lang="ts" setup>
  import { reactive, ref, onMounted } from 'vue';
  import {
    useLogGet,
    useLogDelete,
    cfCarParkSearch,
  } from '@/api/internetOfThings';
  import { getCarTypeListByQuery } from '@/api/park';
  import editAdd from './editAdd.vue';
  import { parseTime } from '@/utils';
  import { cloneDeep } from 'lodash';
  import dayjs from 'dayjs';

  const pagination = reactive({
    current: 1,
    pageSize: 10,
    total: 0,
  });
  const rowSelection = reactive({
    type: 'checkbox',
    showCheckedAll: true,
    fixed: true,
    onlyCurrent: true,
  });
  const selectedKeys = ref([]);
  const loading = ref(false);
  const selectLoading = ref(false);
  const editAddRef = ref();
  const columns = [
  {
      title: 'id',
      dataIndex: 'id',
      width: '190',
    },
    {
      title: '车牌号',
      dataIndex: 'numberPlate',
      width: '100',
    },
    {
      title: '停车场名称',
      dataIndex: 'carParkName',
      width: '150',
    },
    {
      title: '车辆类型',
      dataIndex: 'newCarType',
      width: '100',
    },
    {
      title: '入口名称',
      dataIndex: 'inCheckPointName',
      width: '100',
    },
    {
      title: '出口名称',
      dataIndex: 'outCheckPointName',
      width: '100',
    },
    {
      title: '进场时间',
      dataIndex: 'newInTime',
      width: '180',
    },
    {
      title: '出场时间',
      dataIndex: 'newOutTime',
      width: '180',
    },
    {
      title: '支付时间',
      dataIndex: 'newPayTime',
      width: '180',
    },
    {
      title: '家庭组',
      dataIndex: 'homeGroup',
      width: '100',
    },
    {
      title: '备注',
      dataIndex: 'remarks',
      width: '100',
    },
    {
      title: '入场小图',
      dataIndex: 'inSmallImageUrl',
      slotName: 'inSmallImageUrl',
    },
    {
      title: '入场大图',
      dataIndex: 'inBigImageUrl',
      slotName: 'inBigImageUrl',
    },
    {
      title: '出场小图',
      dataIndex: 'outSmallImageUrl',
      slotName: 'outSmallImageUrl',
    },
    {
      title: '出场大图',
      dataIndex: 'outBigImageUrl',
      slotName: 'outBigImageUrl',
    },
    // {
    //   title: '操作',
    //   dataIndex: 'operations',
    //   slotName: 'operations',
    //   fixed: 'right',
    // },
  ];
  const state = reactive({
    list: [],
  });
  const generateFormModel = () => {
    return {
      numberPlate: '',
      carParkId: '',
      carType: '',
      minOutTime: '',
      maxOutTime: '',
    };
  };
  const formModel = ref(generateFormModel());
  const carTypeList = ref([]);
  const parkList = ref([]);

  onMounted(() => {
    fetchData();
    getCarType();
  });
  const onBatchDelete = async () => {
    console.log(selectedKeys.value);
    await useLogDelete({ params: { ids: selectedKeys.value.join(',') } });
    selectedKeys.value = [];
    fetchData();
  };
  const pageChange = (e: number) => {
    pagination.current = e;
    fetchData();
  };
  const handleAdd = () => {
    editAddRef.value.handleClick();
  };
  const handleEdit = (e: any) => {
    editAddRef.value.handleClick(e);
  };
  const search = () => {
    fetchData();
  };
  const onDelete = async (id: string) => {
    await useLogDelete({ params: { id } });
    fetchData();
  };
  const getCarType = async () => {
    const { data, code, message } = await getCarTypeListByQuery({
      params: {
        page: 1,
        size: 200,
      },
    });
    if (code === 10002) {
      carTypeList.value = data;
    } else {
      // @ts-ignore
      // Message.error(message);
    }
  };
  const reset = () => {
    formModel.value = generateFormModel();
  };
  const handleSelectSearch = async (value) => {
    if (value) {
      selectLoading.value = true;
      const { data, code } = await cfCarParkSearch({
        params: {
          name: value,
          page: 1,
          size: 200,
        },
      });
      selectLoading.value = false;
      if (code === 10002) {
        parkList.value = data;
      } else {
        parkList.value = [];
      }
    } else {
      parkList.value = [];
    }
  };
  const getCarTypeName = (key: string) => {
    // @ts-ignore
    const findRes: any = carTypeList.value.find((i) => i.flagKey === key);
    return findRes?.name;
  };
  const checkParams = (obj: any) => {
    // eslint-disable-next-line no-restricted-syntax, guard-for-in
    for (const i in obj) {
      if (!obj[i]) {
        if (obj[i] !== 0) {
          obj[i] = null;
        }
      }
    }
    return obj;
  };
  const fetchData = async () => {
    loading.value = true;
    let newForm = cloneDeep(formModel.value);
    if (newForm.minOutTime) {
      newForm.minOutTime = dayjs().valueOf() + '';
    }
    if (newForm.maxOutTime) {
      newForm.maxOutTime = dayjs().valueOf() + '';
    }
    // @ts-ignore
    const { data, code, total } = await useLogGet({
      params: checkParams({
        page: pagination.current,
        size: pagination.pageSize,
        ...newForm,
      }),
    });
    loading.value = false;
    if (code === 10002) {
      // @ts-ignore
      if (total) pagination.total = total;
      for (const i of data) {
        i.newCarType = getCarTypeName(i.carType) || '-';
        i.newInTime = parseTime(i.inTime) || '-';
        i.newOutTime = parseTime(i.outTime) || '-';
        i.newPayTime = parseTime(i.payTime) || '-';
      }
      state.list = data;
    } else {
      state.list = [];
    }
    // console.log(data, code, 999);
  };
</script>

<script lang="ts">
  export default {
    name: 'SearchTable',
  };
</script>

<style scoped lang="less">
  .container {
    padding: 0 20px 20px 20px;
  }
</style>
